#include <taskbase.h>

// #####################task hello begin############################
// void getChipid()
// {// stc32g12k128 chipid first read is defferent from read after 1 min sec.....
// #define CHIPID_ADDR (&CHIPID0)
//     char DEV_C51_XDATA_FLAG myChipidBuf[16];
//     memcmp(myChipidBuf, CHIPID_ADDR, 7);
//     logHexBufStr("get chipbuf:", myChipidBuf, 7);
// }
#define taskIntervalMs_hello 60000
static sysTmTp DEV_C51_XDATA_FLAG taskLastUpMs_hello = 0;
#if TASK_USE_INIT_QUIT_FLAG
static void taskFuncInit_hello()
{
    logdebugc("");
}
static void taskFuncQuit_hello()
{
    logdebugc("");
}
#endif
static void taskFuncUp_hello()
{
    logdebugc("taskFuncUp_hello");
    // getChipid();
}
// #####################task hello end############################

// #####################task check1 begin############################
#define taskIntervalMs_check1 60000
static sysTmTp DEV_C51_XDATA_FLAG taskLastUpMs_check1 = 0;
#if TASK_USE_INIT_QUIT_FLAG
static void taskFuncInit_check1()
{
    logdebugc("");
}
static void taskFuncQuit_check1()
{
    logdebugc("");
}
#endif
static void taskFuncUp_check1()
{
    logdebugc("taskFuncUp_check1");
}
// #####################task check1 end############################

typedef enum
{
    taskDoId_hello = 0,
    taskDoId_check1,
    taskDoId_Max,
} taskDoIdEnum;
#if TASK_USE_INIT_QUIT_FLAG
void taskMgr51Init()
{
#define taskDoOneInit(tname)      \
    case taskEnumGet(tname):      \
    {                             \
        taskFuncInitGet(tname)(); \
        break;                    \
    }
    int i = 0;
    for (i = 0; i < taskDoId_Max; i++)
    {
        switch (i)
        {
            taskDoOneInit(hello);
            taskDoOneInit(check1);
        default:
        {
            break;
        }
        }
    }
}

void taskMgr51Quit()
{
#define taskDoOneQuit(tname)      \
    case taskEnumGet(tname):      \
    {                             \
        taskFuncQuitGet(tname)(); \
        break;                    \
    }
    int i = 0;
    for (i = 0; i < taskDoId_Max; i++)
    {
        switch (i)
        {
            taskDoOneQuit(hello);
            taskDoOneQuit(check1);
        default:
        {
            break;
        }
        }
    }
}
#endif
void taskMgr51Run()
{
    static uint8_t tmpGlobalUpId = 0;
    uint8_t i;
    sysTmTp DEV_C51_XDATA_FLAG tmpTmMs;
    tmpTmMs = sysTmNow();
#define taskDoOneUp(tname)                                            \
    case taskEnumGet(tname):                                          \
    {                                                                 \
        if (tmpTmMs - taskLastUpGet(tname) >= taskIntervalGet(tname)) \
        {                                                             \
            taskFuncUpGet(tname)();                                   \
            taskLastUpGet(tname) = sysTmNow();                        \
        }                                                             \
        break;                                                        \
    }
    for (i = 0; i < taskDoId_Max; i++)
    {
        tmpGlobalUpId = CYCLE_ADD(tmpGlobalUpId, i, taskDoId_Max);
        switch (tmpGlobalUpId)
        {
            taskDoOneUp(hello);
            taskDoOneUp(check1);
        default:
        {
            break;
        }
        }
        if (sysTmNow() - tmpTmMs >= TASK_RUN_MAX_MS)
        {
            break;
        }
    }
}
